package com.ie.controller.admin;

import com.ie.common.http.ResponseResult;
import com.ie.common.security.anno.AuthIgrone;
import com.ie.entity.GoodsEntity;
import com.ie.service.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ie.entity.CartEntity;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    private static String fileName;
    private static byte[] fileContent;

    @AuthIgrone
    @PostMapping("/fileInit")
    public void fileInit(@RequestBody MultipartFile file) {
        try {
            fileContent = file.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fileName = file.getOriginalFilename();
    }

    @AuthIgrone
    @PostMapping("/getGoods")
    public ResponseResult<Map<String, Object>> getGoods(@RequestBody GoodsEntity goodsEntity) {
        return goodsService.getGoods(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("/addGoods")
    public ResponseResult<Map<String, Object>> addGoods(@RequestBody GoodsEntity goodsEntity) {
        goodsEntity.setFileName(fileName);
        goodsEntity.setLogoFile(fileContent);
        return goodsService.addGoods(goodsEntity);
    }

    @PostMapping("/deleteGoods")
    public ResponseResult<Map<String, Object>> deleteGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.deleteGoods(goodsEntity);
    }

    @PostMapping("/updateGoods")
    public ResponseResult<Map<String, Object>> updateGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.updateGoods(goodsEntity);
    }

    @AuthIgrone
    @RequestMapping("/getAdvGoods")
    public ResponseResult<List<GoodsEntity>> getAdvGoods(){
        return goodsService.getAdvGoods();
    }

    @AuthIgrone
    @PostMapping("/getGoodsById")
    public ResponseResult<GoodsEntity> getGoodsById(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsById(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("/getGoodsIndex")
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsIndex(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("/cartGoods")
    public ResponseResult<List<Map<String, Object>>> myCartGoods(@RequestBody CartEntity cartEntity){
        return goodsService.myCartGoods(cartEntity);
    }

    @AuthIgrone
    @PostMapping("/getGoodsStore")
    public Integer getGoodsStoreById(GoodsEntity goodsEntity){
        return goodsService.getGoodsStoreById(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("batchUdateGoods")
    public ResponseResult<Map<String, Object>> batchUdateGoods(List<GoodsEntity> list){
        return goodsService.batchUdateGoods(list);
    }
}
